<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Turn;
use App\Models\User;
use App\Models\Currency;
use App\Models\Property;
use Illuminate\Http\Request;

class TurnController extends Controller
{
    public function __construct()
    {
        view()->share('menu', 'turn');
    }
    /**
     * Display a listing of the resource.
     * 第三方充值记录
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $account = $start_time = $end_time = '';
        $data    = Turn::where(['type' => 1])
            ->where(function ($query) use ($request) {
                $account = $request->get('account');
                if (!empty($account)) {
                    $uid = User::where(['account' => $account])->value('id');
                    $query->where(['uid' => $uid]);
                }
            })
            ->where(function ($query) use ($request) {
                $start_time = $request->get('start_time');
                $end_time   = $request->get('end_time');
                if (!empty($start_time) && !empty($end_time)) {
                    $query->whereBetween('created_at', [$start_time, $end_time]);
                } elseif (!empty($start_time) && empty($end_time)) {
                    $query->where('created_at', '>', $start_time);
                } elseif (empty($start_time) && !empty($end_time)) {
                    $query->where('created_at', '<', $end_time);
                }
            })
            ->orderby('created_at', 'desc')
            ->paginate(10);

        $page_num = $data->firstItem();

        return view('admin/turn/index', compact('data', 'page_num', 'account', 'start_time', 'end_time'));
    }

    public function top_up(Request $request)
    {
        $where['pay_switch'] = 1;
        $where['buy_switch'] = 1;
        $infos = Currency::where($where)->get()->toArray();
        return view('admin/currency/top_up', compact('infos'));
    }


    public function top_up_submit(Request $request){

        $info = $request->all();
        if (empty($info['type'])||empty($info['cid'])||empty($info['name'])||empty($info['value'])){
            return ['msg'=>'请填写数据','status'=>0];
        }
        //验证用户
        if ($info['type']==1){
            $where['phone'] = $info['name'];
        }else{
            $where['email'] = $info['name'];
        }
        $user = User::where($where)->first();
        if (empty($user)){
            return ['msg'=>'用户不存在','status'=>0];
        }
        $currency = Currency::where('id',$info['cid'])->first();
        if (empty($currency)){
            return ['msg'=>'货币信息错误','status'=>0];
        }
        $value = (int)$info['value'];
        if ($value>1000000){
            return ['msg'=>'充钱不能超过一百万','status'=>0];
        }
        if($currency->is_standard==2){
            $all_money = Property::where('cid',$info['cid'])->sum('total');
            if ($value>$currency->total||$value>$currency->total-$all_money){
                return ['msg'=>'数量超过平台发行总量','status'=>0];
            }
        }
        $where1['cid'] = $info['cid'];
        $where1['uid'] = $user->id;
        $property = Property::where($where1)->first();
        if (empty($property)){
            $data['cid'] = $info['cid'];
            $data['uid'] = $user->id;
            $data['balance'] = $value;
            $data['total'] = $value;
            $re = Property::insert($data);
        }else{
            $data['balance'] = $value+$property->balance;
            $data['total'] = $value+$property->total;
            $re = $property->update($data);
        }
        if ($re){
            return ['msg'=>'充值成功','status'=>1];
        }else{
            return ['msg'=>'充值失败','status'=>0];
        }
    }

    public function create()
    {
    }

    public function store(Request $request)
    {
    }

    public function show($id)
    {
    }

    public function edit($id)
    {
    }

    public function update(Request $request, $id)
    {
    }

    public function destroy($id)
    {
    }
}
